/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.SaleWellProduct;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.SaleWellProductRepository;
import me.zhengjie.modules.system.service.SaleWellProductService;
import me.zhengjie.modules.system.service.dto.SaleWellProductDto;
import me.zhengjie.modules.system.service.dto.SaleWellProductQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.SaleWellProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李忠臻
* @date 2024-02-29
**/
@Service
@RequiredArgsConstructor
public class SaleWellProductServiceImpl implements SaleWellProductService {

    private final SaleWellProductRepository saleWellProductRepository;
    private final SaleWellProductMapper saleWellProductMapper;

    @Override
    public PageResult<SaleWellProductDto> queryAll(SaleWellProductQueryCriteria criteria, Pageable pageable){
        Page<SaleWellProduct> page = saleWellProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(saleWellProductMapper::toDto));
    }

    @Override
    public List<SaleWellProductDto> queryAll(SaleWellProductQueryCriteria criteria){
        return saleWellProductMapper.toDto(saleWellProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SaleWellProductDto findById(Long id) {
        SaleWellProduct saleWellProduct = saleWellProductRepository.findById(id).orElseGet(SaleWellProduct::new);
//        ValidationUtil.isNull(saleWellProduct.getId(),"SaleWellProduct","id",id);
        return saleWellProductMapper.toDto(saleWellProduct);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SaleWellProduct resources) {
        saleWellProductRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SaleWellProduct resources) {
//        SaleWellProduct saleWellProduct = saleWellProductRepository.findById(resources.getId()).orElseGet(SaleWellProduct::new);
//        ValidationUtil.isNull( saleWellProduct.getId(),"SaleWellProduct","id",resources.getId());
//        saleWellProduct.copy(resources);
//        saleWellProductRepository.save(saleWellProduct);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            saleWellProductRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SaleWellProductDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SaleWellProductDto saleWellProduct : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("型号", saleWellProduct.getModel());
            map.put("距今天数", saleWellProduct.getFromNowDay());
            map.put("采购次数", saleWellProduct.getPurchaseTime());
            map.put("采购数量", saleWellProduct.getQuantity());
//            map.put("创建者", saleWellProduct.getCreateBy());
//            map.put("更新者", saleWellProduct.getUpdateBy());
//            map.put("创建日期", saleWellProduct.getCreateTime());
//            map.put("更新时间", saleWellProduct.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}